package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.adapter.RiskDetailTableFragmentAdapter;
import com.example.safeassistan.home.model.BackLogFragmentModel;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.risk.activity.RiskPointTableDetailActivity;
import com.example.safeassistan.risk.model.RiskPointtableModel;
import com.example.safeassistan.utile.JsonUtils;
import com.example.safeassistan.widget.ViewPagerForScrollView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivitySafeTable extends BaseActivity {
    private RecyclerView rcView;
    private LinearLayout llNodata;

    private RiskDetailTableFragmentAdapter adapter;
    private String riskpointId="";
    private String riskName="";
    private String defId="";
    private int posi=0;


    public static void actionStart(int posi,Context context,String riskpointId,String riskName,String defId) {
        Intent intent = new Intent(context, ActivitySafeTable.class);
        intent.putExtra("posi",posi);
        intent.putExtra("riskName",riskName);
        intent.putExtra("riskpointId",riskpointId);
        intent.putExtra("defId",defId);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.riskdetail_table_fragment;
    }

    public void initView(){
        setTitleText("安全检查表");
        posi=getIntent().getIntExtra("posi",0);
        riskpointId=getIntent().getStringExtra("riskpointId");
        riskName=getIntent().getStringExtra("riskName");
        defId=getIntent().getStringExtra("defId");
        rcView=findViewById(R.id.rcView);
        llNodata=findViewById(R.id.llNodata);
        getData();
    }

    @Override
    protected void initData() {

    }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     *
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }
    public void getData(){
        Map map=new HashMap();
        map.put("id",riskpointId);
        new HttpRequest(ActivitySafeTable.this).doGet(UrlConstants.getRiskPointTable, null, map,new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), RiskPointtableModel.class);
                        if (obj instanceof RiskPointtableModel) {
                            RiskPointtableModel model=(RiskPointtableModel) obj;
                            if (model.getResult().size()>0){
                                llNodata.setVisibility(View.GONE);
                                adapter=new RiskDetailTableFragmentAdapter(model.getResult());
                                rcView.setLayoutManager(new LinearLayoutManager(ActivitySafeTable.this));
                                rcView.setAdapter(adapter);
                                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        List<BackLogFragmentModel.ResultDTO.RecordsDTO.DangerVOListDTO> list=new ArrayList<>();
                                        BackLogFragmentModel.ResultDTO.RecordsDTO.DangerVOListDTO m=null;
                                        if (model.getResult().get(position).getDangerVOList()!=null&&model.getResult().get(position).getDangerVOList().size()>0){
                                            for (int i=0;i<model.getResult().get(position).getDangerVOList().size();i++){
                                                m=new BackLogFragmentModel.ResultDTO.RecordsDTO.DangerVOListDTO();
                                                if (i==0){
                                                    m.setSelect(true);
                                                }else {
                                                    m.setSelect(false);
                                                }
                                                m.setId(model.getResult().get(position).getDangerVOList().get(i).getId());
                                                m.setDangerSrcName(model.getResult().get(position).getDangerVOList().get(i).getDangerSrcName());
                                                list.add(m);
                                            }
                                        }else {
                                            m=new BackLogFragmentModel.ResultDTO.RecordsDTO.DangerVOListDTO();
                                            m.setSelect(true);
                                            m.setId("");
                                            m.setDangerSrcName("");
                                            list.add(m);
                                        }
                                        HiddenDangerinvestigation.actionStart(posi,ActivitySafeTable.this,
                                                defId,
                                                riskName, 1,
                                                riskpointId, model.getResult().get(position).getId(),
                                                list);
                                    }
                                });
                            }else {
                                llNodata.setVisibility(View.VISIBLE);
                            }
                        }
                    }else {
                        Toast.makeText(ActivitySafeTable.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }
}